﻿using System;
using System.Linq;
using System.Collections.Generic;

using XConductor.Domain.Seedwork.Media;

namespace XConductor.Domain.Shared.Media
{
    public class AudioFormatsAdapter : IAudioFormatsAdapter<string>
    {
        private Dictionary<AudioFormats, string> m_format_name;
        private Dictionary<string, AudioFormats> m_name_format;

        public static AudioFormatsAdapter Instance = new AudioFormatsAdapter();

        public AudioFormatsAdapter()
        {
            this.m_format_name = this.CreateMap();
            this.m_name_format = this.CreateOppositeMap(this.m_format_name);
        }

        private Dictionary<AudioFormats, string> CreateMap()
        {
            var dic = new Dictionary<AudioFormats, string>();

            dic.Add(AudioFormats.LinearPCM, "lpcm");
            dic.Add(AudioFormats.AC3, "ac-3");
            dic.Add(AudioFormats.AC3, "cac3");
            dic.Add(AudioFormats.AppleIMA4, "ima4");
            dic.Add(AudioFormats.MPEG4AAC, "aac");
            dic.Add(AudioFormats.MPEG4CELP, "celp");
            dic.Add(AudioFormats.MPEG4HVXC, "hvxc");
            dic.Add(AudioFormats.MPEG4TwinVQ, "twvq");
            dic.Add(AudioFormats.MACE3, "MAC3");
            dic.Add(AudioFormats.MACE6, "MAC6");
            dic.Add(AudioFormats.ULaw, "ulaw");
            dic.Add(AudioFormats.ALaw, "alaw");
            dic.Add(AudioFormats.QDesign, "QDMC");
            dic.Add(AudioFormats.QDesign2, "QDM2");
            dic.Add(AudioFormats.QUALCOMM, "Qclp");
            dic.Add(AudioFormats.MPEGLayer1, ".mp1");
            dic.Add(AudioFormats.MPEGLayer2, ".mp2");
            dic.Add(AudioFormats.MPEGLayer3, ".mp3");
            dic.Add(AudioFormats.TimeCode, "time");
            dic.Add(AudioFormats.MIDIStream, "midi");
            dic.Add(AudioFormats.ParameterValueStream, "apvs");
            dic.Add(AudioFormats.AppleLossless, "alac");
            dic.Add(AudioFormats.MPEG4AAC_HE, "aach");
            dic.Add(AudioFormats.MPEG4AAC_LD, "aacl");
            dic.Add(AudioFormats.MPEG4AAC_ELD, "aace");
            dic.Add(AudioFormats.MPEG4AAC_ELD_SBR, "aacf");
            dic.Add(AudioFormats.MPEG4AAC_HE_V2, "aacp");
            dic.Add(AudioFormats.MPEG4AAC_Spatial, "aacs");
            dic.Add(AudioFormats.AMR, "samr");
            dic.Add(AudioFormats.Audible, "AUDB");
            dic.Add(AudioFormats.iLBC, "ilbc");
            dic.Add(AudioFormats.DVIIntelIMA, "DVIIntelIMA");
            dic.Add(AudioFormats.MicrosoftGSM, "MicrosoftGSM");
            dic.Add(AudioFormats.AES3, "aes3");

            return dic;
        }

        private Dictionary<string, AudioFormats> CreateOppositeMap(Dictionary<AudioFormats, string> map)
        {
            return map
                .ToList()
                .ToDictionary(kvp => kvp.Value, kvp => kvp.Key, StringComparer.OrdinalIgnoreCase);
        }

        public string GetName(AudioFormats format)
        {
            string name = string.Empty;

            this.m_format_name.TryGetValue(format, out name);

            return name;
        }

        public AudioFormats GetFormat(string name)
        {
            AudioFormats format = AudioFormats.Unknown;

            this.m_name_format.TryGetValue(name, out format);

            return format;
        }
    }
}
